﻿using CodeGenerator.DBInfo.entity;
using CodeGenerator.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MybatisPlusGenerator.Dialog
{
    public partial class TableColumnDialog : Form
    {
        public TableColumnDialog(string style, TableInfo tableInfo)
        {
            InitializeComponent();
            var str = "";
            switch (style)
            {
                case "Antd":
                    str = GenAntdStyle(tableInfo);
                    break;
                case "SGTable":
                    str = GenSGTableStyle(tableInfo);
                    break;
            }
            this.content.Text = str;
        }
        /// <summary>
        /// 生成Antd Table样式的列信息
        /// </summary>
        /// <param name="tableInfo">表信息</param>
        /// <returns>列信息json字符串</returns>
        string GenAntdStyle(TableInfo tableInfo)
        {
            List<Dictionary<string, dynamic>> lst = new List<Dictionary<string, dynamic>>();
            foreach(var column in tableInfo.Columns)
            {
                var name = column.Name.ToSmallCamelCase();
                var comment = column.Comment;
                var item = new Dictionary<string, dynamic>();
                item["key"] = name;
                item["dataIndex"] = name;
                item["title"] = (comment.IsNullOrEmpty() ? name : comment).FirstWord() ?? "";
                lst.Add(item);
            }
            return ToJsonNoKeyQuotes(lst);
        }

        /// <summary>
        /// 生成SGTable Table样式的列信息
        /// </summary>
        /// <param name="tableInfo">表信息</param>
        /// <returns>列信息json字符串</returns>
        string GenSGTableStyle(TableInfo tableInfo)
        {
            List<Dictionary<string, dynamic>> lst = new List<Dictionary<string, dynamic>>(); 
            foreach (var column in tableInfo.Columns)
            {
                var item = new Dictionary<string, dynamic>();
                var name = column.Name.ToSmallCamelCase();
                var comment = column.Comment;
                item["key"] = name;
                item["value"] = (comment.IsNullOrEmpty() ? name : comment).FirstWord() ?? "";
                if (column.IsPrimaryKey)
                {
                    item["primaryKey"] = true;
                    item["hidden"] = true;
                }
                lst.Add(item);
            }
            return ToJsonNoKeyQuotes(lst);
        }
        /// <summary>
        /// 转为json字符串但无key引号
        /// </summary>
        /// <param name="data">对象</param>
        /// <returns></returns>
        string ToJsonNoKeyQuotes(object data)
        {
            using(var sw = new StringWriter())
            using (var writer = new JsonTextWriter(sw) { QuoteName=false,Formatting=Formatting.Indented })
            {
                var serializer = new JsonSerializer();
                serializer.Serialize(writer, data);
                return sw.ToString();
            }
        }
    }
}
